import Cookies from 'js-cookie'

const app = {
  state: {
    menu: {
    },
    sidebar: {
      opened: Cookies.get('studio-sidebarStatus') === 'true'
    },
    ws: {
      connection: false,
      statusChange: {},
      notification: {}
    }
  },
  mutations: {
    TOGGLE_CONNECT_STATUS: (state, isConnected) => {
      state.ws.connection = isConnected
    },
    UPDATE_DEVICE_CHANGE_STATUS: (state, statusChange) => {
      state.ws.statusChange = statusChange
    },
    UPDATE_NOTIFICATION: (state, newNotification) => {
      state.ws.notification = newNotification
    },
    TOGGLE_SIDEBAR: state => {
      state.sidebar.opened = !state.sidebar.opened
      Cookies.set('factory-sidebarStatus', state.sidebar.opened)
    },
    UPDATE_CURRENT_MENU: (state, menu) => {
      state.menu = menu
    }
  },
  actions: {
    updateWsConnection ({
      commit
    }, isConnected) {
      return new Promise(resolve => {
        commit('TOGGLE_CONNECT_STATUS', isConnected)
        resolve()
      })
    },
    updateStatus ({
      commit
    }, statusChange) {
      return new Promise(resolve => {
        commit('UPDATE_DEVICE_CHANGE_STATUS', statusChange)
        resolve()
      })
    },
    updateNotification ({ commit }, newNotification) {
      return new Promise(resolve => {
        commit('UPDATE_NOTIFICATION', newNotification)
        resolve()
      })
    },
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    updateCurrentMenu ({ commit }, menu) {
      return new Promise(resolve => {
        commit('UPDATE_CURRENT_MENU', menu)
        resolve()
      })
    }
  }
}

export default app
